package com.dili.dd.gpi.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dili.dd.gpi.domain.Application;
import com.dili.dd.gpi.domain.Event;
import com.dili.dd.gpi.service.DmaAppService;
import com.dili.dd.gpi.service.DmaEventManageService;
import com.dili.dd.gpi.util.Pagination;
import com.dili.dd.gpi.util.spring.JsonView;
import com.dili.dd.gpi.util.spring.ObjectConvertAnno;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoYuan on 2015/10/15.
 */
@Controller
@RequestMapping("/dma/event")
public class DmaEventController {

    @Resource
    private DmaEventManageService dmaEventManageService;

    @Resource
    private DmaAppService dmaAppService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String toList(Model model,@RequestParam(required = true)String appId){
        Application application = dmaAppService.getAppById(appId);
        model.addAttribute("app",application);
        return "/WEB-INF/views/dma/event/list";
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView list(String appkey,@ObjectConvertAnno Pagination pagination){
        List<Event> events = dmaEventManageService.queryEventByAppkey(appkey, pagination);
        Map<String,Object> model = new HashMap<>();
        model.put("pagination",pagination);
        model.put("data", events);
        return new ModelAndView(new JsonView(model, SerializerFeature.WriteMapNullValue));
    }

    @RequestMapping(value = "/save.htm",method = RequestMethod.PUT)
    public ModelAndView add(Event event){
        dmaEventManageService.addEvent(event);
        return new ModelAndView(new JsonView(true,"创建成功"));
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ModelAndView update(Event event){
        dmaEventManageService.updateEvent(event);
        return new ModelAndView(new JsonView(true,"修改成功"));
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public ModelAndView update(String appkey,String eventId){
        dmaEventManageService.deleteEvent(appkey, eventId);
        return new ModelAndView(new JsonView(true,"删除成功"));
    }
}
